package com.hk.core.web.interceptors;

import com.hk.core.web.Webs;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.AsyncHandlerInterceptor;


/**
 * 在异步中，获取不到request，将 requestAttributes 放在 NamedInheritableThreadLocal 中上
 *
 * @author Kevin
 * @date 2018-11-30 09:27
 */
public class AsyncRequestHandlerInterceptor implements AsyncHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        ServletRequestAttributes requestAttribute = Webs.getRequestAttribute();
        RequestContextHolder.setRequestAttributes(requestAttribute, true);
        return true;
    }
}
